<?php



class LoginAction extends Action
{
	public function register()
	{
		$user_dao = new UserModel();
		$user['id'] = $user_dao->addUser($_POST['name'], $_POST['password']);
		$user['name'] = $_POST['name'];
		$_SESSION['user'] = $user;
		$this->redirect("../Index/index");
	}
	
	public function login()
	{
		$name = $_POST['name'];
		$password = $_POST['password'];
		$user_dao = M('user');
		$condition['name'] = $name;
		$condition['password'] = $password;
		$user = $user_dao->where($condition)->find();
		if($user == null) {
			$this->redirect("loginPage");
		}
		else {
			$_SESSION['user'] = $user;
			$this->redirect("../Index/index");
		}
	}
	
	public function adminLogin() 
	{
		$name = $_POST['name'];
		$password = $_POST['password'];
		$admin_dao = M('admin');
		$condition['name'] = $name;
		$condition['password'] = $password;
		$admin = $admin_dao->where($condition)->find();
		if($admin == null) {
			$this->redirect("adminLoginPage");
		}
		else {
			$_SESSION['admin'] = $admin;
			$this->redirect("Index/admin");
		}
	}
	
	public function logout() {
		unset($_SESSION['user']);
		$this->redirect("../Index/index");
	}
}

?>